<?php
/**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * LICENSE: LGPL
 *
 * @category   Sabai
 * @package    Sabai_URL
 * @copyright  Copyright (c) 2006 myWeb Japan (http://www.myweb.ne.jp/)
 * @license    http://opensource.org/licenses/lgpl-license.php GNU LGPL
 * @version    CVS: $Id: URL.php 168 2008-03-11 19:13:33Z mikhail.miguel $
 * @link
 * @since      File available since Release 0.1.5
*/
/**
 * URL scheme
 */
define('SABAI_URL_PROTOCOL', !empty($_SERVER['HTTPS']) ? 'https://' : 'http://');
/**
 * Server host name
 */
define('SABAI_URL_HOST', !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost');
/**
 * Server URL
 */
define('SABAI_URL_SERVER', SABAI_URL_PROTOCOL . SABAI_URL_HOST);
/**
 * Script URL
 */
define('SABAI_URL_SCRIPT', SABAI_URL_SERVER .  $_SERVER['SCRIPT_NAME']);
/**
 * URL for script directory
 */
define('SABAI_URL_DIR', SABAI_URL_SERVER . '/' . trim(dirname($_SERVER['SCRIPT_NAME']), "/\\"));
/**
 * URL
 */
define('SABAI_URL', SABAI_URL_SERVER . $_SERVER['PHP_SELF']);

define('SABAI_URL_METHOD_NORMAL', 0);
define('SABAI_URL_METHOD_PATHINFO', 1);

/**
 * Short description for class
 *
 * Long description for class (if any)...
 *
 * @category   Sabai
 * @package    Sabai_URL
 * @copyright  Copyright (c) 2006 myWeb Japan (http://www.myweb.ne.jp/)
 * @author     Kazumi Ono <onokazu@gmail.com>
 * @license    http://opensource.org/licenses/lgpl-license.php GNU LGPL
 * @version    CVS: $Id: URL.php 168 2008-03-11 19:13:33Z mikhail.miguel $
 * @link
 * @since      Class available since Release 0.1.5
 */
class Sabai_URL
{
    /**
     * Defines the way URL are generated
     *
     * @static
     * @param int $method
     */
    function method($method)
    {
        if (!defined('SABAI_URL_METHOD')) {
            define('SABAI_URL_METHOD', $method);
        }
    }

    /**
     * Generates a URL
     *
     * @static
     * @param string $base
     * @param array $params
     * @param string $fragment
     * @param string $dir
     * @return string
     */
    function get($base = '', $params = array(), $fragment = '', $script = SABAI_URL_SCRIPT, $queries = array())
    {
        if (!empty($params)) {
            foreach ($params as $k => $v) {
                if (is_array($v)) {
                    $k .= '[]';
                    foreach ($v as $_v) {
                        array_push($queries, urlencode($k) . '=' . urlencode($_v));
                    }
                } else {
                    array_push($queries, urlencode($k) . '=' . urlencode($v));
                }
            }
        }
        $query = implode('&amp;', $queries);
        switch (@SABAI_URL_METHOD) {
            case SABAI_URL_METHOD_PATHINFO:
            	$url = !empty($query) ? $script . $base . '?' . $query : $script . $base;
            	break;
            default:
            	$url = $script . '?' . $query . '&amp;q=' . urlencode($base);
            	break;
        }
        if (!empty($fragment)) {
            return $url . '#' . $fragment;
        }
        return $url;
    }

    /**
     * Generates a URL from an array of options.
     *
     * @static
     * @param array $options
     * @return string
     */
    function getByArray($options = array())
    {
        $default = array('base'     => '',
                         'params'   => array(),
                         'fragment' => '',
                         'script'   => SABAI_URL_SCRIPT,
                         'query'    => array());
        $options = array_merge($default, $options);
        return Sabai_URL::get($options['base'], $options['params'], $options['fragment'], $options['script'], $options['query']);
    }

    /**
     * Generates a URL
     *
     * @static
     * @param string $script
     * @param string $base
     * @param array $params
     * @param string $fragment
     * @return string
     */
    function getByScript($script = 'index.php', $base = '', $params = array(), $fragment = '')
    {
        return Sabai_URL::get($base, $params, $fragment, SABAI_URL_DIR. '/' . $script);
    }
}